package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;

import com.hzjq.core.bean.Param;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.databinding.ActivitySettingControlPanelBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.SettingControlPanelPresenter;

public class SettingControlPanelActivity extends BasePresenterActivity<SettingControlPanelPresenter, ActivitySettingControlPanelBinding> implements SettingController.ControlPanelView {

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingControlPanelActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivitySettingControlPanelBinding getBindingView() {
        return ActivitySettingControlPanelBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.btnRead.setOnClickListener(view -> mPresenter.getSettingParams());
        binding.btnWrite.setOnClickListener(view -> new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_note)
                .setMessage("确定修改控制板参数吗？")
                .setListener(dialog -> mPresenter.setSettingParams(getString(binding.etConnectVoltage),
                        getString(binding.etChargeVoltage),
                        getString(binding.etQueryVoltage),
                        getString(binding.etChargeAdc),
                        getString(binding.etOutageDelay),
                        getString(binding.etGroupingCount),
                        getString(binding.etGroupingInterval),
                        getString(binding.etSingleCheck),
                        getString(binding.etReservedOne),
                        getString(binding.etReservedTwo),
                        getString(binding.etReservedThree),
                        getString(binding.etReservedFour))).show());
    }


    @Override
    protected SettingControlPanelPresenter createPresenter() {
        return new SettingControlPanelPresenter(new SettingModel(), this);
    }

    @Override
    public void showSettingParams(Param param) {
        binding.etConnectVoltage.setText(String.valueOf(param.getSignalVoltage()));
        binding.etChargeVoltage.setText(String.valueOf(param.getChargeVoltage()));
        binding.etQueryVoltage.setText(String.valueOf(param.getQueryVoltage()));
        binding.etChargeAdc.setText(String.valueOf(param.getChargeAdc()));
        binding.etOutageDelay.setText(String.valueOf(param.getBlastCutPower()));
        binding.etGroupingCount.setText(String.valueOf(param.getGroupChargeCount()));
        binding.etGroupingInterval.setText(String.valueOf(param.getGroupChargeSleepTime()));
        binding.etSingleCheck.setText(String.valueOf(param.getReserve1()));
        binding.etReservedOne.setText(String.valueOf(param.getBlastAdc()));
        binding.etReservedTwo.setText(String.valueOf(param.getReserve2()));
        binding.etReservedThree.setText(String.valueOf(param.getReserve3()));
        binding.etReservedFour.setText(String.valueOf(param.getReserve4()));
        showNoteDialog();
    }

    private void showNoteDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("参数读取成功")
                .show();
    }
}